﻿package com.game.data.vo
{
    import base.BaseVO;
    
    import com.dusk.game.HackChecker;
    import com.dusk.util.ArrayUtil;
    import com.game.data.constant.ConfigConst;
    import com.game.data.constant.MaxAttribConst;
    
    import config.Config;
    
    public class PlayerDataVO extends BaseVO
    {
        
        private const allTypeEquip:Vector.<String> = new <String>["equipList", "stuffList", "consumeList"];
        
        public function PlayerDataVO()
        {
            super();
            hasStudySkill = {
                xfLevel: [0, 0],
                skillLevel: [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
            }
            wearEquip = new WearEquipVO();
            petList = new <PetDataVO>[]();
        }
        
        //角色自定义名称
        public var playerName:String = "造梦使者";
        //角色id，1-4分别对应悟空、唐僧、八戒、沙僧
        public var roleId:uint = 0;
        //装备存储
        public var bagEquipList:Array = [];
        //道具存储
        public var bagStuffList:Array = [];
        //时装存储
        public var bugConsumeList:Array = [];
        //当前装备存储
        public var wearEquip:WearEquipVO;
        //宠物存储
        public var petList:Vector.<PetDataVO>;
        //已经学习的技能心法及等级
        public var hasStudySkill:Object = {
            xfLevel: [0, 0],
            skillLevel: [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
        };
        //当前穿戴的技能，内容为SkillVO
        public var equippedSkill:Array = [];
        //被动技能
        public var passiveSkill:Array = [0, 0, 0, 0, 0];
        //是否显示时装
        public var isShowFashion:Boolean = true;
        //经验值
        public var exp:int = 0;
        //技能数量上限
        public var skillLimit:int = 5;
        //灵魂值
        public var lhValue:int = 100000;
        //等级
        public var level:int = 1;
        
        public function addLhValue(value:int):void
        {
            lhValue += value;
        }
        
        public function reduceLhValue(value:int):void
        {
            lhValue -= value;
        }
        
        /**
         * 增加经验值
         * @param value
         */
        public function addExp(value:int):void
        {
            exp += value;
        }
        
		//=============================技能管理接口=================================
        {
            /**
             * 升级/学习 完技能后将技能等级同步到已经装备的技能列表中
             */
            public function updateEquippedSkill():void
            {
                var vec:Vector.<SkillVO> = gc.configManager.skillProxy.getAllSkillByRoleId(roleId);
                for each(var skill:SkillVO in vec)
                {
                    var vo:SkillVO = getEquippedSkillBySkillId(skill.skillId);
                    if (vo == null)
                    {
                        continue;
                    }
                    vo.skillLevel = hasStudySkill.skillLevel[skill.belongGroup - 1][skill.groupIndex - 1];
                }
            }
            
            /**
             * 通过索引获取到已经装备的技能对象
             * @param index 索引，从1开始，1-5
             * @return SkillVO
             */
            public function getEquippedSkillByIndex(index:int):SkillVO
            {
                for each(var skill:SkillVO in equippedSkill)
                {
                    if (skill.skillIndex == index)
                    {
                        return skill;
                    }
                }
                return null;
            }
            
            /**
             * 按照指定的skillIndex移除指定位置的技能
             * @param index 技能索引，1-5
             */
            public function removeEquippedSkillByIndex(index:int):void
            {
                for (var i:int = equippedSkill.length - 1; i >= 0; i--)
                {
                    if (equippedSkill[i].skillIndex == index)
                    {
                        equippedSkill.splice(i, 1);
                    }
                }
                updateEquippedSkill();
            }
            
            /**
             * 按照指定的skillIndex移除技能
             * @param id 技能索引，1-5
             */
            public function removeEquippedSkillBySkillId(id:String):void
            {
                for (var i:int = equippedSkill.length - 1; i >= 0; i--)
                {
                    if (equippedSkill[i].skillId == id)
                    {
                        equippedSkill.splice(i, 1);
                    }
                }
                updateEquippedSkill();
            }
            
            /**
             * 通过技能ID获取到正在装备的技能对象
             * @param id skillId，如火魔斩为hmz
             * @return SkillVO
             */
            public function getEquippedSkillBySkillId(id:String):SkillVO
            {
                for each(var skill:SkillVO in equippedSkill)
                {
                    if (skill.skillId == id)
                    {
                        return skill;
                    }
                }
                return null;
            }
            
            /**
             * 通过技能ID获取到已经学习的技能对象，无论是否装备
             * @param id
             * @return
             */
            public function getStudiedSkillBySkillId(id:String):SkillVO
            {
                var vo:SkillVO = gc.configManager.skillProxy.getSkillById(id);
                vo.skillLevel = hasStudySkill.skillLevel[vo.belongGroup - 1][vo.groupIndex - 1];
                return vo;
            }
            
            /**
             * 判断是否已经学习过该技能，无论是否装备
             * @param id
             * @return
             */
            public function getHasStudySkillById(id:String):Boolean
            {
                var vo:SkillVO = gc.configManager.skillProxy.getSkillById(id);
                return hasStudySkill.skillLevel[vo.belongGroup - 1][vo.groupIndex - 1] > 0;
            }
            
            /**
             * 判断是否装备该技能
             * @param id
             * @return
             */
            public function getHasEquipSkillById(id:String):Boolean
            {
                for each (var skill:SkillVO in equippedSkill)
                {
                    if(skill.skillId == id)
                    {
                        return true;
                    }
                }
                return false;
            }
            
            /**
             * 寻找那个技能位置是空的，用于添加新的技能
             * @return 剩余位置组成的数组
             */
            public function findWhichSkillIndexIsEmpty():Array
            {
                var index:Array = [1, 2, 3, 4, 5];
                for each(var skill:SkillVO in equippedSkill)
                {
                    if (ArrayUtil.contains(index, skill.skillIndex))
                    {
                        ArrayUtil.removeElement(index, skill.skillIndex);
                    }
                }
                return index;
            }
            
            /**
             * 获取已经学习的技能总数
             * @return int
             */
            public function getAllStudiedSkillNum():int
            {
                var total:int = 0;
                for each(var arr:Array in hasStudySkill.skillLevel)
                {
                    for each(var num:int in arr)
                    {
                        if (num > 0)
                        {
                            total++;
                        }
                    }
                }
                return total;
            }
        }
        //=============================技能管理接口=================================
        
        //=============================装备管理接口=================================
        {
            public function putOnEquip(vo:EquipmentVO):void
            {
                if(ArrayUtil.contains(bagEquipList,vo))
                {
                    ArrayUtil.removeElement(bagEquipList,vo,false);
                }
                wearEquip.putOn(vo);
            }
            
            public function takeOffEquip(vo:EquipmentVO):void
            {
                bagEquipList.push(vo);
                wearEquip.takeOff(vo);
            }
        }
        //=============================装备管理接口=================================
        
        public function getRoleName():String
        {
            const _map:Object = {
                1: "悟空",
                2: "唐僧",
                3: "八戒",
                4: "沙僧",
                5: "白龙"
            };
            return _map[roleId];
        }
        
        public function getEquipAppearance():Object
        {
            var id:Object = wearEquip.getShowId();
            if(isShowFashion)
            {
                id.weapon = id.fashionWeapon > 0 ? id.fashionWeapon : id.weapon;
                id.cloth = id.fashionCloth > 0 ? id.fashionCloth : id.cloth;
                id.wing = id.fashionWing > 0 ? id.fashionWing : id.wing;
            }
            id.weapon = id.displayWeapon > 0 ? id.displayWeapon : id.weapon;
            id.cloth = id.displayCloth > 0 ? id.displayCloth : id.cloth;
            id.wing = id.displayWing > 0 ? id.displayWing : id.wing;
            delete id.displayWeapon;
            delete id.displayCloth;
            delete id.displayWing;
            delete id.fashionWeapon;
            delete id.fashionCloth;
            delete id.fashionWing;
            return id;
        }
        
        /**
         * 根据装备名称获取装备
         * @param equipId 装备id
         * @return 装备vo
         */
        public function getEquipInBagByName(equipId:String):EquipmentVO
        {
            for each(var equipType:String in allTypeEquip)
            {
                for each (var item:EquipmentVO in this[equipType])
                {
                    if (item.equipId == equipId)
                    {
                        return item;
                    }
                }
            }
            return null;
        }
        
        /**
         * 获取当前装备栏中指定类型的装备
         * @param  equipType 装备类型
         * @return 装备vo
         */
        public function getCurEquipByType(equipType:String):EquipmentVO
        {
            for each(var equip:EquipmentVO in wearEquip.getAllEquip())
            {
                if (equip.type == equipType)
                {
                    return equip;
                }
            }
            return null;
        }
        
        /**
         * 从当前装备栏移除装备
         * @param equipVO 要移除的装备vo
         */
        public function removeCurEquip(equipVO:EquipmentVO):void
        {
            if (ArrayUtil.contains(wearEquip.getAllEquip(), equipVO))
            {
                wearEquip.takeOff(equipVO);
            }
        }
        
        /**
         * 从背包里面移除装备
         * @param equipId 要移除的装备的代码
         * @param removeType 移除模式
         *                 1.数量 -1,若在装备栏中则删除
         *                 2.按照参数3的数量移除道具
         *                 4.按照参数3的数量移除技能书
         * @param removeNum 需要移除的数量
         */
        public function removeEquipFormBag(equipId:String, removeType:int, removeNum:uint = 1):void
        {
            var _loc4_:int = 0;
            var _loc5_:EquipmentVO = this.getEquipInBagByName(equipId);
            if (removeType == 1)
            {
                if (_loc5_.num > 1)
                {
                    _loc5_.modifyNum(-1);
                }
                else if ((_loc4_ = this.bagEquipList.indexOf(_loc5_)) != -1)
                {
                    this.bagEquipList.splice(_loc4_, 1);
                }
            }
            else if (removeType == 2)
            {
                if (_loc5_.num > removeNum)
                {
                    _loc5_.modifyNum(-removeNum);
                }
                else if ((_loc4_ = this.bagStuffList.indexOf(_loc5_)) != -1)
                {
                    this.bagStuffList.splice(_loc4_, 1);
                }
            }
            else if (removeType == 4)
            {
                if (_loc5_.num > removeNum)
                {
                    _loc5_.modifyNum(-removeNum);
                }
            }
        }
        
        /**
         * 获取某件道具的数量
         * @param equipId 装备id
         * @return 数量
         */
        public function getStuffNumByName(equipId:String):int
        {
            for each (var equip:EquipmentVO in bagStuffList)
            {
                if (equip.equipId == equipId)
                {
                    return equip.num;
                }
            }
            return 0;
        }
        
        /**
         * 设置当前装备栏中指定类型的装备
         * @param equipType 装备类型
         * @param equipVO 装备vo
         */
        public function setCurEquipByType(equipType:String, equipVO:EquipmentVO):void
        {
//            var union:Array = [wearEquip, getCurRoleData().privateEquipList];
//            for each (var equipList:Array in union)
//            {
//                for (var i:int = 0; i < equipList.length; i++)
//                {
//                    if (equipType == equipList[i].type)
//                    {
//                        equipList[i] = equipVO;
//                        return;
//                    }
//                }
//            }
        }
        
        public function exportToSaveData():Object
        {
            var data:Object = {};
            data.playerIndex = roleId;
            data.lhValue = lhValue;
            data.exp = exp;
            data.level = level;
            data.skillLimit = skillLimit;
            data.roleId = roleId;
            data.playerName = playerName;
            data.isShowFashion = isShowFashion;
            
            data.passiveSkill = passiveSkill;
            data.petList = getPetSaveData();
            //统一声明变量
            var equipVO:EquipmentVO;
            //导出当前装备的数据
            data.curEquipList = [];
            data.equipList = [];
            for each (equipVO in bagEquipList)
            {
                data.equipList.push(equipVO.exportToSaveData());
            }
            //导出背包道具数据
            data.stuffList = [];
            for each (equipVO in bagStuffList)
            {
                data.stuffList.push(equipVO.exportToSaveData());
            }
            //导出背包时装数据
            data.fashionList = [];
            for each (equipVO in bugConsumeList)
            {
                data.fashionList.push(equipVO.exportToSaveData());
            }
            return data;
        }
        
        public function importFromSaveData(data:Object):void
        {
            roleId = data.playerIndex;
            lhValue = data.lhValue;
            exp = data.exp;
            level = data.level;
            skillLimit = data.skillLimit;
            roleId = data.roleId;
            playerName = data.playerName;
            isShowFashion = data.isShowFashion;
            
            passiveSkill = data.passiveSkill;
            petList = parsePetSaveData(data.petList);
            
            var parseEquipment:Function = function (sourceArr:Array, targetArr:Array):void
            {
                for each(var equipVO:Object in sourceArr)
                {
                    var newEquipVO:EquipmentVO = gc.allEquip.findByName(equipVO.equipId);
                    newEquipVO.importFromSaveData(equipVO);
                    targetArr.push(newEquipVO);
                }
            }
            parseEquipment(data.equipList, bagEquipList);
            parseEquipment(data.stuffList, bagStuffList);
            parseEquipment(data.fashionList, bugConsumeList);
            //检测作弊
            if (this.checkCheat())
            {
                HackChecker.hackHandler();
            }
        }
        
        /**
         * 根据index获取被动等级
         * @param idx 1-5
         * @return 1-maxLevel
         */
        public function getPassiveSkillLevelByIndex(idx:int):int
        {
            return passiveSkill[idx - 1];
        }
        
        public function returnSkillLevelBySkillName(param1:String):int
        {
            /*var _loc2_:uint = this.skillbykey.length;
            var _loc3_:* = ["slz", "zz", "sx", "qsez", "hmz", "lys", "hytj", "lyfb", "jdy", "hyjj", "sgq", "myhc", "jgz", "tjgl", "jhsj", "blb", "xbz", "shy", "sjt", "smb", "dj", "sd", "rj", "zznh", "syzq", "ssp", "jsp", "dgq", "xgq", "tmc", "zq", "mbyj", "wdww", "jdz", "mds", "qlj", "tkj", "dzj", "lybj", "mmw"];
            while (_loc2_-- > 0)
            {
                if (this.skillbykey[_loc2_].skillName == param1)
                {
                    if (this.skillbykey[_loc2_].slev == undefined || int(this.skillbykey[_loc2_].slev) == 0)
                    {
                        this.skillbykey[_loc2_].slev = 1;
                    }
                    if (_loc3_.indexOf(param1) != -1)
                    {
                        if (this.skillbykey[_loc2_].slev > 10)
                        {
                            this.skillbykey[_loc2_].slev = 1;
                            return 1;
                        }
                        return this.skillbykey[_loc2_].slev;
                    }
                    this.skillbykey[_loc2_].slev = 1;
                    return 1;
                }
            }*/
            return 1;
        }
        
        /**
         * 查找当前战斗中的宠物
         * @param containDead 是否包含彻底死亡的宠物
         * @return 宠物数据
         */
        public function findCurrentPet(containDead:Boolean = false):PetDataVO
        {
            for each (var i:PetDataVO in petList)
            {
                if (i.isFight == 1)
                {
                    if (i.lifetime > 0)
                    {
                        return i;
                    }
                    if (containDead)
                    {
                        return i;
                    }
                }
            }
            return null;
        }
        
        public function findBiggestPetLevel():int
        {
            var maxLv:int = 0;
            for each (var i:PetDataVO in petList)
            {
                if (i.level > maxLv)
                {
                    maxLv = i.level;
                }
            }
            return maxLv;
        }
        
        public function catchNewPet(petName:String, petLevel:uint = 1):Boolean
        {
            if (this.petList.length >= ConfigConst.GAME_PET_MAX_SEAT)
            {
                return false;
            }
            var pf:PetDataVO = new PetDataVO();
            pf.setPetNameAndLevel(petName, petLevel);
            this.petList.push(pf);
            return true;
        }
        
        public function reSetAllPetState():void
        {
            for each (var i:PetDataVO in this.petList)
            {
                i.resetPetState();
            }
        }
        
        private function getPetSaveData():Array
        {
            var arr:Array = [];
            for each(var i:PetDataVO in this.petList)
            {
                arr.push(i.cloneToObject());
            }
            return arr;
        }
        
        private function parsePetSaveData(data:Array):Vector.<PetDataVO>
        {
            var parData:Vector.<PetDataVO> = new Vector.<PetDataVO>();
            for each (var i:String in data)
            {
                var pf:PetDataVO = new PetDataVO();
                pf.updateFromObject(JSON.parse(i));
                parData.push(pf);
            }
            return parData;
        }
        
        private function checkCheat():Boolean
        {
            if (lhValue < 0 || lhValue > ConfigConst.GAME_ROLE_MAXVALUE)
            {
                return true;
            }
            if (level < 0 || level > MaxAttribConst.MaxRoleLevel)
            {
                return true;
            }
            if (exp < 0 || exp > ConfigConst.GAME_ROLE_MAXVALUE)
            {
                return true;
            }
            return false;
        }
        
        private function get gc():Config
        {
            return Config.getIns();
        }
    }
}

import com.game.data.constant.ConfigConst;
import com.game.data.constant.MaxAttribConst;

import flash.system.System;

class WatcherFunction
{
    public static function watchLhValue(oldValue:int, newValue:int):void
    {
        if (newValue < 0 || newValue > ConfigConst.GAME_ROLE_MAXVALUE)
        {
            throw new Error("请勿使用修改器");
        }
    }
    
    public static function watchLevel(oldValue:int, newValue:int):void
    {
        if (newValue < 0 || newValue > MaxAttribConst.MaxRoleLevel)
        {
            throw new Error("请勿使用修改器");
        }
    }
}